package cn.iocoder.yudao.module.wengfu.controller.admin.consignmentprocessingreport;

import cn.iocoder.yudao.module.wengfu.controller.admin.resultsdispatchroomdetails.vo.exportDto;
import cn.iocoder.yudao.module.wengfu.enums.ReportEnums;
import cn.iocoder.yudao.module.wengfu.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.wengfu.framework.excel.core.util.wengfu.ReportDataProcessService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.math.BigDecimal;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.wengfu.controller.admin.consignmentprocessingreport.vo.*;
import cn.iocoder.yudao.module.wengfu.dal.dataobject.consignmentprocessingreport.ConsignmentProcessingReportDO;
import cn.iocoder.yudao.module.wengfu.service.consignmentprocessingreport.ConsignmentProcessingReportService;

@Tag(name = "管理后台 - 委托加工及购进矿石质量报表")
@RestController
@RequestMapping("/wengfu/consignment-processing-report")
@Validated
public class ConsignmentProcessingReportController {

    @Resource
    private ConsignmentProcessingReportService consignmentProcessingReportService;

    @Resource
    private ReportDataProcessService ReportDataProcessService;

    @PostMapping("/create")
    @Operation(summary = "创建委托加工及购进矿石质量报表")
    @PreAuthorize("@ss.hasPermission('wengfu:consignment-processing-report:create')")
    public CommonResult<Long> createConsignmentProcessingReport(@Valid @RequestBody ConsignmentProcessingReportSaveReqVO createReqVO) {
        return success(consignmentProcessingReportService.createConsignmentProcessingReport(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新委托加工及购进矿石质量报表")
    @PreAuthorize("@ss.hasPermission('wengfu:consignment-processing-report:update')")
    public CommonResult<Boolean> updateConsignmentProcessingReport(@Valid @RequestBody ConsignmentProcessingReportSaveReqVO updateReqVO) {
        consignmentProcessingReportService.updateConsignmentProcessingReport(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除委托加工及购进矿石质量报表")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wengfu:consignment-processing-report:delete')")
    public CommonResult<Boolean> deleteConsignmentProcessingReport(@RequestParam("id") Long id) {
        consignmentProcessingReportService.deleteConsignmentProcessingReport(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除委托加工及购进矿石质量报表")
                @PreAuthorize("@ss.hasPermission('wengfu:consignment-processing-report:delete')")
    public CommonResult<Boolean> deleteConsignmentProcessingReportList(@RequestParam("ids") List<Long> ids) {
        consignmentProcessingReportService.deleteConsignmentProcessingReportListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得委托加工及购进矿石质量报表")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wengfu:consignment-processing-report:query')")
    public CommonResult<ConsignmentProcessingReportRespVO> getConsignmentProcessingReport(@RequestParam("id") Long id) {
        ConsignmentProcessingReportDO consignmentProcessingReport = consignmentProcessingReportService.getConsignmentProcessingReport(id);
        return success(BeanUtils.toBean(consignmentProcessingReport, ConsignmentProcessingReportRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得委托加工及购进矿石质量报表分页")
    @PreAuthorize("@ss.hasPermission('wengfu:consignment-processing-report:query')")
    public CommonResult<PageResult<ConsignmentProcessingReportRespVO>> getConsignmentProcessingReportPage(@Valid ConsignmentProcessingReportPageReqVO pageReqVO) {
        PageResult<ConsignmentProcessingReportDO> pageResult = consignmentProcessingReportService.getConsignmentProcessingReportPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ConsignmentProcessingReportRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出委托加工及购进矿石质量报表 Excel")
    @PreAuthorize("@ss.hasPermission('wengfu:consignment-processing-report:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportConsignmentProcessingReportExcel(@Valid ConsignmentProcessingReportPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
//        //更新一份委托加工及购进矿石质量报表数据
        ReportDataProcessService.processAndSaveReportData();
        //委托加工及购进矿石质量报表数据来源是购进磷矿石数据源
        List emptyList = new ArrayList();
        // 使用 EasyExcel 导出，注册公式处理器。
        // 我们需要自适应多级表头和列，所以我们不能直接用原生方法导出数据，所以传入空dto，exportDto.class,并且传入一个空的list2
        ExcelUtils.writeWithFormula(response, "委托加工及购进矿石质量报表.xlsx", ReportEnums.EXPORT_PRODUCT_QUALITY_SUMMARY.getReportName(),
                exportDto.class, emptyList);
    }

}
